package com.example.newspapersmanageos.Controller;


import com.example.newspapersmanageos.Entity.Page;
import com.example.newspapersmanageos.Entity.User;
import com.example.newspapersmanageos.Service.OperationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;


@Controller
@RequestMapping("/operation")
public class OperationController {
    @Autowired
    private OperationService operationService;

    //删除操作
    @RequestMapping(value = "/delete/{id}")
    public String delete(@PathVariable int id, HttpServletRequest request) {
        boolean b = operationService.deleteUserById(id);
        if (b) {
            return "redirect:/loginContorller/homePage";
        }
        return "error";
    }

    //通过用户名查询用户
    @RequestMapping(value = "/serchUserByName",method=RequestMethod.POST)
    public String searchUserByName(@RequestParam("userName") String userName,HttpServletRequest request,@RequestParam(value = "page", required = true, defaultValue = "1") String page1) {
        int pageNum = 1, pageSize = 5;
        if(page1!=null && !page1.equals("")) {
            pageNum = Integer.parseInt(page1);
        }
        if (!userName.equals("")) {
            request.getServletContext().setAttribute("serchUserByName",userName );
            Page<User> page = operationService.searchUserByName(userName,pageNum,pageSize);
            request.setAttribute("page", page);
            return "home";
        }
        return "redirect:/loginContorller/homePage";
    }

    //修改用户名
    @RequestMapping(value = "/update/{id}", method=RequestMethod.GET)
    public String updateById(@PathVariable("id") int id,
                             HttpServletRequest request) {
        request.setAttribute("userManage", "updateById");
//        User u = operationService.searchUserById(id);
//        request.setAttribute("user", u);
        return "updateForm";
    }
}
